/* Standard libraries */
#include <stdio.h>
#include <string.h>

/* Own includes */
#include "../include/sceneloader.h"
#include "../include/charloader.h"

/* Internal prototypes */
void scene_load_background(xmlDocPtr scenexml, xmlNodePtr node);
void scene_load_colpol(xmlDocPtr scenexml, xmlNodePtr node);

int scene_load(char * scenenumber)
{
    xmlDocPtr scenexml = NULL;
    xmlNodePtr node = NULL;

    /* Assemble the xml filename */
    char * xmldocpath = malloc(sizeof(scenenumber) + 21);
    memset(xmldocpath, '\0', sizeof(xmldocpath));
    strcat(xmldocpath, "xml/scenes/scene");
    strcat(xmldocpath, scenenumber);
    strcat(xmldocpath, ".xml");

    /* Load the xml file */
    scenexml = xml_loaddoc(xmldocpath);

    /* Load the background */
    scene_load_background(scenexml, node);

    /* Load the collision polygon */
    scene_load_colpol(scenexml, node);

    /* Free the no longer necessary file name and xml file */
    free(xmldocpath);
    xml_freedoc(scenexml);

	return 0;
}

void scene_load_background(xmlDocPtr scenexml, xmlNodePtr node)
{
    /* Get the right node */
    node = xml_getroot(scenexml, "scene");
    node = xml_getnode(node, "graphics->background");

    /* Load the background */
    char * bgimage = xml_readnode(scenexml, node, "image");
    int bgposx = atoi(xml_readnode(scenexml, node, "posx"));
    int bgposy = atoi(xml_readnode(scenexml, node, "posy"));

    /* Malloc space for SceneBg image */
    imgSceneBg = malloc(sizeof(*imgSceneBg));

    /* Pass it the values */
    imgSceneBg->img = load_bitmap(bgimage, 0);
    imgSceneBg->x = bgposx;
    imgSceneBg->y = bgposy;

    /* Free the image path */
    free(bgimage);
}

void scene_load_colpol(xmlDocPtr scenexml, xmlNodePtr node)
{
    int x = 0, y = 0;

    /* Get the right node */
    node = xml_getroot(scenexml, "scene");

    /* Get the total number of points in the polygon */
    colTotal = atoi(xml_readattrib(scenexml, node, "collision", (xmlChar *)"total"));

    colX = malloc(sizeof(float) * colTotal);
    colY = malloc(sizeof(float) * colTotal);
    memset(colX, '0', sizeof(float) * colTotal);
    memset(colY, '0', sizeof(float) * colTotal);

    /* Now get the collision node and start filling our array */
    node = xml_getnode(node, "collision");

    for(node = node->children; node != NULL; node = node->next) {
        if(!xmlStrcmp(node->name, (const xmlChar *)"text"))
            continue;

        if(!xmlStrcmp(node->name, (const xmlChar *)"x")) {
            colX[x] = atof(xmlNodeListGetString(scenexml, node->xmlChildrenNode, 1));
            x++;
        } else
        if(!xmlStrcmp(node->name, (const xmlChar *)"y")) {
            colY[y] = atof(xmlNodeListGetString(scenexml, node->xmlChildrenNode, 1));
            y++;
        } else continue;

        printf("%s\n", node->name);
    }
}

void scene_draw()
{
    blit(imgSceneBg->img, buffer, 0, 0, imgSceneBg->x, imgSceneBg->y, imgSceneBg->img->w, imgSceneBg->img->h);
}

void scene_cleanup()
{
    destroy_bitmap(imgSceneBg->img);
    free(imgSceneBg);
    free(colX);
    free(colY);
}
